package com.tvt.sexyapp_demo.appconfig.manager;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.tvt.sexyapp_demo.R;
import com.tvt.sexyapp_demo.appconfig.reader.AppConfigReader;
import com.tvt.sexyapp_demo.appconfig.xml.AppConfigXml;
import com.tvt.sexyapp_demo.appconfig.xml.XItem;
import com.tvt.sexyapp_demo.appconfig.xml.XSMSConfig;
import com.tvt.sexyapp_demo.debug.Debug;

/**
 * @author Huy Thanh
 * 
 */
public class PageInfoManager {

	// Constant
	private static final String LOG_TAG = "PageInfoManager";
	private final String CONFIG_FILENAME = "ConfigApp.xml";

	// Fields
	private final int MIN_PAGE_NUMBER = 1;
	private int MAX_PAGE_NUMBER;

	private AppConfigXml mAppConfigXml;
	private XSMSConfig mSMSConfig;
	
	private int mCurrIndex = 0;
	private int mCurrPageNumber = MIN_PAGE_NUMBER;

	private Bitmap mNoImageBitmap;

	private PageInfoManager() {
	}

	public static PageInfoManager getInstance() {
		return SingletonHolder.INSTANCE;
	}

	public void intInfo(final Context pContext) {

		if (pContext != null) {
			
			this.mAppConfigXml = AppConfigReader.decodeAppConfig(pContext,
					this.CONFIG_FILENAME);

			this.MAX_PAGE_NUMBER = AppConfigReader
					.getMaxPageNumber(this.mAppConfigXml);

			this.mNoImageBitmap = BitmapFactory.decodeResource(
					pContext.getResources(), R.drawable.default_image);
			
			
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					mSMSConfig = AppConfigReader.decodeSMSConfig(pContext);
					
				}
			});
			thread.start();
			try {
				thread.join();
				if(this.mSMSConfig!=null) {
					this.mAppConfigXml.getStartApp().setActiveSMS(this.mSMSConfig.isActiveSMS());
					this.mAppConfigXml.getRunningApp().setDisplayConfirmPopup(this.mSMSConfig.isDisplayConfirmPopup());
				}
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
			

		} else
			Debug.e(LOG_TAG, "Can't init info because null context!");
	}

	public ArrayList<XItem> getItemsOnPage() {
		return AppConfigReader.readPageItems(this.mAppConfigXml,
				this.mCurrPageNumber);
	}

	public AppConfigXml getAppConfig() {
		return mAppConfigXml;
	}

	public Bitmap getDefaultPicture() {
		return mNoImageBitmap;
	}

	public int getMinPageNumber() {
		return MIN_PAGE_NUMBER;
	}

	public int getMaxPageNumber() {
		return MAX_PAGE_NUMBER;
	}

	public int getCurrentPageNumber() {
		return mCurrPageNumber;
	}

	public int getCurrentIndex() {
		return mCurrIndex;
	}

	public void setCurrentIndex(int pIndex) {
		mCurrIndex = pIndex;
	}

	public void setCurrentPageNumber(int pPageNumber) {
		mCurrPageNumber = pPageNumber;
	}

	// Inner Classes
	private static class SingletonHolder {
		public static final PageInfoManager INSTANCE = new PageInfoManager();
	}
}
